<?php
  
  echo "Includer v 0.20 (9.2.2008) by Adam Horcica (c) 2008\n\n";

  if($argc < 3) die("Arguments: source_file target_file [vars_list] [--no-filters|-nf]");
  
  list($_, $source, $target, $vars_list, $no_fillters) = $argv;
  
  $filtering = !(($no_fillters == '--no-filters') || ($no_fillters == '-nf'));
  
  $dir = dirname(realpath($source)) . DIRECTORY_SEPARATOR;
  
  function reg_replace_vars($a)
  {
    global $vars;
    $var_name = $a[1];
        
    if(isset($vars[$var_name]))
    {
      echo " - replacing ". $a[1] . "\n";
      return $vars[$var_name];
    }
    else
    {
      return $a[0];
    }
  }
  
  function repaceVars($text)
  {
    return preg_replace_callback('~\$\$_(\w+)_\$\$~U', 'reg_replace_vars', $text);
  }
 
  echo "Source:  ".basename($source)." in $dir\n".
       "Target:  $target \n".
       "Dir:     ".getcwd()."\n".
       "Filters: ".(($filtering)?'Yes':'No').
       "\n\n";

  if(file_exists($vars_list))
  {
    $vars = parse_ini_file($vars_list, false);
    echo "Variable list '$vars_list': \n";
    
    foreach ($vars as $key=>$value) {
      echo " * $key = \"$value\"\n";	
    }

  }
  else
  {
    $vars = array();
  }
  
  echo "\n\nOpening source file '$source'.\n";
  
  $content = file_get_contents($source);
  
  $ok_counter = 0; $err_counter = 0; 
  
  function read_file($file, $del = '')
  {
    global $dir, $ok_counter, $err_counter;
    $f = @file($file);
    if($f === false){
      echo "ERROR: File '$file' not found.\n"; $err_counter ++;
      return "";
    }
    echo "Including file '$file' \n"; $ok_counter ++;
    $text = $del . implode($del, $f);
    $text = repaceVars($text);
    return $text;
  }
  
  function include_file($data){
    global $dir, $ok_counter, $err_counter;
    
    // print_r($data);
    list($_, $del, $file) = $data;
    $file = $dir . trim($file, ' \/');
    $text = read_file($file, $del);
    return $text;
  }
  
  function include_file_filter($data){
    global $dir, $ok_counter, $err_counter, $filtering;
    
    list($_, $del, $filter, $file) = $data;
    
    $filter = strtolower(trim($filter));
    $file = $dir . trim($file, ' \/');
    
    if(!$filtering)
    {
      return read_file($file, $del);
    }
    
    $filter_src = dirname(__FILE__) . DIRECTORY_SEPARATOR . $filter . ".filter";
    
    echo "Including file '$file' with fitler " . strtoupper($filter) . "\n";
    
    $output = array();
    $return_var = -1;
    
    exec($filter_src . ' "' . $file . '"', $output, $return_var);
    
    if($return_var != 0)
    {
      echo "ERROR: Filter resturn error code $return_var, included without filter.";
      return read_file($file, $del);
    }
    
    $text = $del . implode("\n" . $del, $output);
    $ok_counter++;
    
    $text = repaceVars($text);
    return $text;
    
  }
  
  $content = repaceVars($content);
  
  $content = preg_replace_callback('~^([ \t]*)\$\$ INCLUDE ([A-Z_0-9]{1,10}) (.+) \$\$~mU', 'include_file_filter', $content);
  
  $result = preg_replace_callback('~^([ \t]*)\$\$ INCLUDE (.+) \$\$~mU', 'include_file', $content);
  
  echo "Including done ($ok_counter files included, $err_counter errors).\n";

  $res = file_put_contents($target, $result);
    
  if($res === false){
    echo "ERROR: Target '$target' is not saved well.";
  } else {
    echo "\nTarget saved (" . realpath($target) . " size " . $res . "B)";
  }
  
  
